/* *******************************************************************************
 * Add by  : Samlin.zhang 
 * Add date: 2006/06/28
 * Description:  The Js function for ListArchiveRule.jsp 
 * *********************************************************************************************
 */

// *******  Dojo Init Start ***********
  dojo.require("dojo.dnd.*");
  dojo.require("dojo.event.*");
  dojo.provide("dojo.dnd.TableDropTarget");

  dojo.dnd.TableDropTarget = function(node, types){
    dojo.dnd.HtmlDropTarget.call(this, node, types);
  }
  dojo.inherits(dojo.dnd.TableDropTarget, dojo.dnd.HtmlDropTarget);

  function byId(id){
    return document.getElementById(id);
  }

  function init(){
    // list one
    var dl = byId("rules");
    new dojo.dnd.TableDropTarget(dl, ["tr"]);
    var lis = dl.getElementsByTagName("tr");
    for(var x=1; x<lis.length-2; x++){
      new dojo.dnd.HtmlDragSource(lis[x], "tr");
    }
  }

  var dropTarget = -1;
  dojo.event.connect(dojo, "loaded", "init");
  dojo.lang.extend(dojo.dnd.TableDropTarget, {
    onDragMove: function(e, dragObjects){
      if(!this.checkDroppable(e)) { return; }    
      dropTarget = this.childBoxes[this._getNodeUnderMouse(e)].node;
      return dojo.dnd.HtmlDropTarget.prototype.onDragMove.call(this, e, dragObjects);
    },
    onDrop: function(e){
      if(!this.checkDroppable(e)) { return false; }
      return dojo.dnd.HtmlDropTarget.prototype.onDrop.call(this, e);
    },
    onDropEnd: function(e){
      /************************************************************************
      * IMPORTANT: Need to modify onDropEnd in HtmlDragManager.js to pass the "e" !!!  *
      ************************************************************************/
      if(!this.checkDroppable(e)) { return false; }
      showProgress(true);
      EmailRuleEngineListHelper.dragRule(getDragInfo,e.dragSource.domNode.id,dropTarget.id,ruleType);
//alert("Drag: <"+e.dragSource.domNode.tagName+" id="+e.dragSource.domNode.id+">\nDrop: <"+dropTarget.tagName+" id="+dropTarget.id+">\n");
    },
    checkDroppable: function(e){
      var i = this._getNodeUnderMouse(e);
      // Ignore the table header row
      if( i < 1 || i > this.childBoxes.length-2 ){ return false; }
      // TODO: Ignore the pagerIndex row
      return true;
    }
  });
// *******  Dojo Init End ***********

var enable;
var moveUp;

function isIE()
{
	if(window.ActiveXObject){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		return true;
		} else if(window.XMLHttpRequest){
			xmlhttp=new XMLHttpRequest();
			return false;
	}
}

function getCurrentForm(){
   var frm;
    for (var i = 0; i < document.forms.length; i++) {
    if (document.forms[i].name != "") {
      frm = document.forms[i];
      break;
    }
  }
  return frm;
}

function getSelectedIDs(){
  var allID="";
  var frm=getCurrentForm();
   for (var i = 0; i < frm.elements.length; i++) {
     if (frm.elements[i].name == "basicSelectedID") {
       if (frm.elements[i].checked) {
         hasChecked = true;
        if (allID==""){
          allID=frm.elements[i].value;
        } else{
          allID=allID+"_"+frm.elements[i].value;
        }
      }
    }
  }
  return allID;
}

function getSelectedIDsForEnable(enabledflag){
    var allID="";
    var frm=getCurrentForm();
     for (var i = 0; i < frm.elements.length; i++) {
       if (frm.elements[i].name == "basicSelectedID") {
         if (frm.elements[i].checked) {

          var enableBox=document.getElementById('c'+frm.elements[i].value);
          var pass=((!enableBox.checked && enabledflag ) || (enableBox.checked && !enabledflag ) ) ;
           if (!pass){
              continue ;
            }

          if (allID==""){
            allID=frm.elements[i].value;
          } else{
            allID=allID+"_"+frm.elements[i].value;
          }
        }
      }
    }
  return allID;
}

function checkIsSelectedOne(){
    var hasChecked = false;
    var selectedCount = 0;
    frm=getCurrentForm();
    for (var i = 0; i < frm.elements.length; i++) {
      if (frm.elements[i].name == "basicSelectedID") {
        if (frm.elements[i].checked) {
          selectedCount++;
        }
      }
    }

    if (selectedCount==0) {
      alert(MSG_NO_RECORD_SELECTED);
      return(false);
    }else if(selectedCount>1){
      alert("Only can select one Rule");
      return(false);
    }
    return true;
}

function OpAjaxEnableRule(frm,enableflag){
  enable=enableflag;
  if (!checkIsSelectedOne()){
    return ;
  }
  showProgress(true);

  var allID=getSelectedIDsForEnable(enableflag);
  EmailRuleEngineListHelper.enableBatchRule(getAjaxEnableInfo,allID,enable,ruleType);
}

function getAjaxEnableInfo(content){
  if (content=="N"){
    alert('Update the Eabled Error');
    return;
  }
    var frm =getCurrentForm();
    var hasChecked = false;
    var confirmStr = "";
    for (var i = 0; i < frm.elements.length; i++) {
   // alert(frm.elements[i].name);
      if (frm.elements[i].name == "basicSelectedID") {
        if (frm.elements[i].checked) {
          hasChecked = true;
          //alert('this is from option'+frm.elements[i].value);
          var td=document.getElementById("td"+frm.elements[i].value);
         var enableBox=document.getElementById('c'+frm.elements[i].value);
          //alert(td.innerText);
          if (enable){
            enableBox.checked=true;
            //td.innerHTML="<input checked type='checkbox' name='' value=''  disabled ='true' >";
          }else{
            enableBox.checked=false;
            //td.innerHTML="<input   type='checkbox' name='' value=''  disabled ='true'>";
          }
        }
      }
    }

showProgress(false);
  }

function onEditRule(){
    if (!checkIsSelectedOne()){
    return ;
  }
  var selectID=getSelectedIDs();

  if (selectID.indexOf("_")>-1) {
    alert("<bean:message key='email_archive.rule.only_one_select_rule'/>");
    return ;
    }
  window.location.href="MaintEmailArchiveRule.do?navMode=E&ID="+selectID+"&ruleType="+ruleType;
}

function showProgress(showflag){
   if (FindObj('iframeLoading')) {
     if (showflag){
       FindObj('iframeLoading').style.visibility='visible';
     }else{
      FindObj('iframeLoading').style.visibility='hidden';
    }
   }
  }

function OpAjaxUpRule(frm, moveUpflag){
  if (!checkIsSelectedOne()){
    return ;
  }

  moveUp=moveUpflag;
  var allID=getSelectedIDs();
  showProgress(true);

  if (moveUp){
    EmailRuleEngineListHelper.upBatchRule(getAjaxUpInfo,allID,moveUp,ruleType);
  }else{
    EmailRuleEngineListHelper.upBatchRule(getAjaxDownInfo,allID,moveUp,ruleType);
  }
}

function getAjaxUpInfo(content){
  if (content=="N"){
    alert('Update the Eabled Error');
    showProgress(false);
    return;
  }

  frm=getCurrentForm();
  if (isIE()==true){
	  for (var i = 0; i < frm.elements.length; i++) {
	    if (frm.elements[i].name == "basicSelectedID") {
	      if (frm.elements[i].checked) {
	        //alert('frm.elements[i].value'+frm.elements[i].value);
	        index=getSelectTableIndex(frm,frm.elements[i].value);
	        if (index>1){
	          exchangeRow(index,moveUp);
	        }
	      }
	    }
	  }
 	}else{ //not IE
		exchangeRowForNotIE();	
	}
  showProgress(false);
}

function getAjaxDownInfo(content){
  if (content=="N"){
    alert('Update the Eabled Error');
    showProgress(false);
    return;
  }
  frm=getCurrentForm();
  if (isIE()==true){
	  for (var i = frm.elements.length-1; i >=0; i--) {
	    if (frm.elements[i].name == "basicSelectedID") {
	      if (frm.elements[i].checked) {
	        index=getSelectTableIndex(frm,frm.elements[i].value);
	        var rowCount=mainTableID.rows.length;
	        if (!moveUp && index<rowCount-2){
	          exchangeRow(index,moveUp);
	        }
	      }
	    }
	  } 
	}else{ //not IE
	  exchangeRowForNotIE(true);  	
	}
  showProgress(false);
}

function exchangeRowForNotIE(down){
  var elementTmp;
  for (var i = frm.elements.length-1; i >=0; i--) {
    elementTmp=frm.elements[i];
    if (elementTmp.name == "basicSelectedID") {
      if (elementTmp.checked) {
        var dl = byId("rules");
        var lis = dl.getElementsByTagName("tr");
        var forwardTd;
        for(var x=0; x<lis.length; x++){
        	
          if (lis[x].id && lis[x].id!="" && lis[x].id==elementTmp.value){
            if (x>0){
              forwardTd=lis[x].innerHTML;
              if(down){
                lis[x].innerHTML=lis[x+1].innerHTML;
                lis[x+1].innerHTML=forwardTd;
              }else{
                lis[x].innerHTML=lis[x-1].innerHTML;
                lis[x-1].innerHTML=forwardTd;
              }
            }       
          }
        }
      }
    }
  }
}
  
	//for getting index by table row
function getSelectRowIndex(frm,ruleID){
      for (i=0; i < document.all.mainTableID.rows.length; i++) {
        var td=document.all.mainTableID.rows[i];
        for (j=0; j < document.all.mainTableID.rows(i).cells.length; j++) {
            var cell= document.all.mainTableID.rows(i).cells(j);
            if (cell.id && cell.id!="" && cell.id==ruleID){
              return i;
            }
        }
      }
}

//for getting index by div table
function getSelectTableIndex(frm,ruleID){
  var dl = byId("rules");
  var lis = dl.getElementsByTagName("tr");

    for(var x=0; x<lis.length; x++){
      if (lis[x].id && lis[x].id!="" && lis[x].id==ruleID){
         return x;
      }
    }
}

function exchangeRow(moveIndex,moveflag){
  var exchangeCellIndex=2;
  if (moveflag){
    shiftRow(moveIndex, -1);
    //var tempValue=mainTableID.rows(moveIndex-1).cells(exchangeCellIndex).innerText;
    //mainTableID.rows(moveIndex-1).cells(exchangeCellIndex).innerText=mainTableID.rows(moveIndex).cells(exchangeCellIndex).innerText;
    // mainTableID.rows(moveIndex).cells(exchangeCellIndex).innerText=tempValue;
  }else{
    shiftRow(moveIndex, 1);
    //var tempValue=mainTableID.rows(moveIndex+1).cells(exchangeCellIndex).innerText;
    //mainTableID.rows(moveIndex+1).cells(exchangeCellIndex).innerText=mainTableID.rows(moveIndex).cells(exchangeCellIndex).innerText;
    //mainTableID.rows(moveIndex).cells(exchangeCellIndex).innerText=tempValue;
    
  }
}

function shiftRow(shiftIndex, shiftTo){
  mainTableID.moveRow(shiftIndex+shiftTo, shiftIndex);
}

function showRuleDescription(ruleDescription,ruleName){
    var detailDescription = "rule name: "+ruleName+"<br>"+ruleDescription
    ruleDescriptionSpan.innerHTML = detailDescription;
  }
  
function getDragInfo(){
  showProgress(false);
}

  